Amarkal.settings._clearNotices   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 9.4285
1
/**
2
 * Asynchronously save all settings in the current settings page to the database.
3
 * Shows an error notification if errors occur. Shows a success notification
4
 * otherwise.
5
 * 
6
 * @param {Function} done
7
 */
8
Amarkal.settings.save = function( done ) 
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
9
{
10
    Amarkal.settings._postData('save',function(res){
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
11
        
12
        $('#amarkal-settings-form').amarkalUIForm('setData', res.values, res.errors);
13
        Amarkal.settings._clearNotices();
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
14
        
15
        if(!$.isEmptyObject(res.errors)) {
16
            for(var name in res.errors) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
17
                var $comp = $('[amarkal-component-name="'+name+'"]'),
18
                    props = $comp.amarkalUIComponent('getProps');
19
                
20
                $comp.amarkalUIComponent('makeInvalid');
21
                $comp.parent()
22
                     .children('.amarkal-settings-error')
23
                     .addClass('amarkal-visible')
24
                     .html(res.errors[name]);
25
26
                Amarkal.settings.notifier.error('Some errors have occured, see below for more information.');
27
                Amarkal.settings.sections.flag('error', props.section);
28
                Amarkal.settings.fields.flag('error', props.name);
29
            }
30
            
31
        }
32
        else {
33
            Amarkal.settings.notifier.success('Settings saved', 3000);
34
        }
35
        done();
36
    });
37
};
38
39
/**
40
 * Asynchronously reset all settings in the current settings page to their 
41
 * default values and erase all data from the database. Shows a success 
42
 * notification upon completion.
43
 * 
44
 * @param {Function} done
45
 */
46
Amarkal.settings.resetAll = function( done ) 
47
{
48
    Amarkal.settings._postData('reset_all',function(res){
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
49
        
50
        $('#amarkal-settings-form').amarkalUIForm('setData', res.values, res.errors);
51
        Amarkal.settings._clearNotices();
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
52
        Amarkal.settings.notifier.success('Default settings applied', 3000);
53
        
54
        done();
55
    });
56
};
57
58
/**
59
 * Asynchronously reset all settings in the current settings section to their 
60
 * default values and erase all section data from the database. Shows a success 
61
 * notification upon completion.
62
 * 
63
 * @param {Function} done
64
 */
65
Amarkal.settings.resetSection = function( done ) 
66
{
67
    Amarkal.settings._postData('reset_section',function(res){
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
68
        
69
        $('#amarkal-settings-form').amarkalUIForm('setData', res.values, res.errors);
70
        Amarkal.settings._clearNotices();
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
71
        Amarkal.settings.notifier.success('Default settings applied for the section <strong>'+Amarkal.settings.sections.getTitle(Amarkal.settings.sections.activeSection)+'</strong>', 3000);
72
        
73
        done();
74
    });
75
};
76
77
/**
78
 * Reset all components and clear errors
79
 */
80
Amarkal.settings._clearNotices = function()
81
{
82
    // Reset all components
83
    $('.amarkal-ui-component').amarkalUIComponent('reset');
84
    $('.amarkal-settings-error').removeClass('amarkal-visible').html('');
85
    Amarkal.settings.sections.unflagAll();
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
86
    Amarkal.settings.fields.unflagAll();
87
};
88
89
/**
90
 * Send serialized form data to be processed in the backend by the function given
91
 * in the 'action' variable.
92
 * 
93
 * @param {string} action
94
 * @param {Function} done
95
 */
96
Amarkal.settings._postData = function( action, done )
97
{
98
    var data = $('#amarkal-settings-form').amarkalUIForm('getData');
99
    $('#amarkal-settings-form').find('input[name^="_amarkal"]').each(function(){
100
        data[$(this).attr('name')] = $(this).val();
101
    });
102
103
    // Set the active section (if applicable)
104
    data._amarkal_settings_section = Amarkal.settings.sections.activeSection;
0 ignored issues
show
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
105
106
    $.post(ajaxurl, {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
107
        action: 'amarkal_settings_'+action,
108
        data: data
109
    }, done);
110
};